#ifndef FILE_H
#define FILE_H

#include <iostream>
#include <vector>

using std::string;
using std::vector;

class File
{
public:
    File(string name, string path) {
        
        string type = "";
        size_t point_index = name.find('.');
        if (point_index == string::npos) {
            name_ = name;
            type_ = type;
        }
        else {
            name_ = name.substr(0, point_index);
            type_ = name.substr(point_index + 1, name.length()-point_index-1);
        }  
        path_ = path;
        content_ = "";
        attribute_ = 0x04;
        start_block_ = 0;
        size_ = 0;
    }

    File(string name, string type, int attribute, int start_block, int size) {
        name_ = name;
        attribute_ = attribute;
        start_block_ = start_block;
        size_ = size;
        type_ = type;
    }

    File() {

    }
    
    static string GetParentDir(string path_name) {
        int i;
        for (i = path_name.length(); i >= 0; i --) {
            if (path_name[i] == '/') {
                break;
            }
        }
        if (i == 0) {
            return "/";
        }
        return path_name.substr(0, i);
    }
    
    static string GetName(string path_name) {
        string parent_dir = GetParentDir(path_name);
        if (parent_dir.length() == 1) {
            return path_name.substr(1, path_name.length()-1);
        }
        else {
            return path_name.substr(parent_dir.length()+1, path_name.length()-parent_dir.length()-1);
        }
    }
    
    static string GetPathName(string path, string name) {
        if (path == "/") {
           return "/"+ name;
        }
        else {
            return path + "/" + name;
        }
    }
    
    string GetPathName() {
        string result = "";
        if (path_ == "/") {
           result = result + "/"+ name_;
        }
        else {
           result = result + path_ + "/" + name_;
        }
        
        if (type_ == "") {
            return result;
        }
        else {
            return result + "." + type_;
        }
    }
    
    string GetNameType() {
        if (type_ == "") {
            return name_;
        }
        else {
            return name_+"."+type_;
        }
    }
    
    static bool FileNameCompare(const File &a, const File &b) {
        return a.name_.compare(b.name_) > 0;
    }
    
    void ClearCache() {
        this->content_ = "";
    }
    
    bool IsEmpty() {
        return name_ == "$";
    }
    
    bool IsDir() {
        return attribute_ == 0xC;
    }

    vector<string> GetPath();
    
    void ChangeToEmptyDirItem() {
        name_ = "$";
        path_ = "";
        content_ = "";
        type_ = "   ";
        attribute_ = 0;
        start_block_ = 0;
        size_ = 0;
    }
    
    bool IsSystemFile() {
        return ((attribute_ >> 1) & 1) == 1;
    }
    
    bool IsReadOnly() {
        return (attribute_ & 1) == 1;
    }
    
    void SetReadOnly(bool value) {
       attribute_ = (attribute_ & value) | value;
    }
   

    string name_;
    string path_;
    string content_;
    string type_;

    /**
     * @brief 文件属性，0目录？0普通文件？0系统文件？0只读？普通目录文件0x0C 普通文件0x4
     */
    int attribute_;
    int start_block_;
    int size_;

private:
    std::vector<int> used_blocks_;
};

#endif // FILE_H
